<!-- 项目审批 -->
<template>
	<view>
		<cu-custom bgColor="bg-blue2" :isBack="true" @back="backedPage"><block slot="backText">返回</block><block slot="content">移库明细编辑</block></cu-custom>
	<scroll-view style="height: 100%" scroll-y="true">
		<form @submit="onSubmit">
			<view class="cu-form-group">
				<view class="title">名称</view>
				<input v-model="item.inStockGoodsDetail.goodsName" placeholder="" name="input" @focus="selectGoods()"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">规格</view>
				<input v-model="item.inStockGoodsDetail.spec" placeholder="" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">产地</view>
				<input v-model="item.inStockGoodsDetail.maker" placeholder="" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">型号</view>
				<input v-model="item.inStockGoodsDetail.model" placeholder="" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">材质</view>
				<input v-model="item.inStockGoodsDetail.materialCode" placeholder="" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">原货位</view>
				<input v-model="item.goodsPosiOrg" placeholder="" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">新货位</view>
				<input v-model="item.goodsPosiNew" placeholder="" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">原数量</view>
				<input v-model="item.qtyOriginal" placeholder="" name="input">{{item.inStockGoodsDetail.qtyUnit}}</input>
			</view>
			<view class="cu-form-group">
				<view class="title">原重量</view>
				<input v-model="item.weightOriginal" placeholder="" name="input">{{item.inStockGoodsDetail.weightUnit}}</input>
			</view>
			<view class="cu-form-group">
				<view class="title">可移数量</view>
				<input v-model="item.qtyAvailable" placeholder="" name="input">{{item.inStockGoodsDetail.qtyUnit}}</input>
			</view>
			<view class="cu-form-group">
				<view class="title">可移重量</view>
				<input v-model="item.weightAvailable" placeholder="" name="input">{{item.inStockGoodsDetail.weightUnit}}</input>
			</view>
			<view class="cu-form-group">
				<view class="title">移库数量</view>
				<input v-model="item.qtyActual" placeholder="" name="input">{{item.inStockGoodsDetail.qtyUnit}}</input>
			</view>
			<view class="cu-form-group">
				<view class="title">移库重量</view>
				<input v-model="item.weightActual" placeholder="" name="input">{{item.inStockGoodsDetail.weightUnit}}</input>
			</view>
			<view>
				<button form-type="submit" class="cu-btn block bg-blue margin-tb-sm lg" :disabled="loading" type="">
					<text :class="loading ? 'cuIcon-loading2 cuIconfont-spin' : ''"></text>保存</button>
			</view>
		</form>
		<view class="cu-modal drawer-modal justify-start" :class="isShowGoodsDialog ? 'show' : ''" @tap="hideModal">
			<view class="cu-dialog basis-lg" @tap.stop="" >
				<select-goods style="height: 95%" :goodsName="item.inStockGoodsDetail.goodsName" @selected="selectedGoods" />
			</view>
		</view>
	</scroll-view>
	</view>
	
</template>

<script>
	import uniTag from '@/components/uni-tag/uni-tag.vue'
	import uniIcons from '@/components/uni-icons/uni-icons.vue'
	import uniCombox from '@/components/uni-combox/uni-combox.vue'
	import selectGoods from './selectGoods.vue'
	import {
		mapGetters
	} from 'vuex'
	import {
		getRandomArrayElements,
		filePreview,
		formatStatus
	} from '@/utils/index.js'
	import { formatDate } from '@/utils/datetime.js'
	export default {
		components: {
			uniTag,
			uniIcons,
			uniCombox,
			selectGoods
		},
		computed: mapGetters(['user']),
		data() {
			return {
				item: { inStockGoodsDetail: {goodsName:''} },
				detailId: 0,
				goodsPosiChngBillId:0,
				weightMethodOptions:['抄码','过地磅','过电子磅','理算','平均件重'],
				workGroupOptions:[],
				machineOptions:[],
				goodsPosiChngTypeOptions:['','吊装','人工自装'],
				ordTypeOptions: ['客户提单','物资调拨单','电子订单'],
				data: [],
				defaultProps: {
					children: 'children',
					label: 'fullName'
				},
				isShowGoodsDialog: false,
				loading: false
			}
		},
		onLoad(options) {

			this.detailId = options.detailId
			this.goodsPosiChngBillId = options.goodsPosiChngBillId
			if (this.detailId >0 && this.detailId > 0) {
				this.$minApi.goodsPosiChngBill(this.goodsPosiChngBillId).then(res => {
					const data = res
					if (data) {
						const detailId = Number(this.detailId)
						const detail =  data.goodsPosiChngDetails.find(f => f.id === detailId)

						if (detail) {
							this.item = detail
						}
					}
				}).catch(err => {
					console.log(err)
				}).finally(() => {
				})
			} else {
				uni.setNavigationBarTitle({
				title: '新增出库明细'
				})
			}
						
			this.$minApi.getWorkGroups().then(res => {
				const data = res
				if (data) {
					this.workGroupOptions =	data.map(a => a.name)
				}
			}).catch(err => {
				console.log(err)
			}).finally(() => {
				this.isLoading = false
			})
			
			this.$minApi.getMachines().then(res => {
				const data = res
				if (data) {
					this.machineOptions = data.map(a => a.name)
				}
			}).catch(err => {
				console.log(err)
			}).finally(() => {
				this.isLoading = false
			})

		},
		methods: {
			filePreview,
			formatStatus,
			formatDate,
			onSubmit(e) {  
				
				this.loading = true
				const _this = this
				
				this.item.qtyOriginal = Number(this.item.qtyOriginal)
				this.item.weightOriginal = Number(this.item.weightOriginal)
				this.item.qtyAvailable = Number(this.item.qtyAvailable)
				this.item.weightAvailable = Number(this.item.weightAvailable)
				this.item.qtyActual = Number(this.item.qtyActual)
				this.item.weightActual = Number(this.item.weightActual)
				this.item.goodsPosiChngBillId =  Number(this.goodsPosiChngBillId)
				
				if (this.detailId > 0) {					
					
					_this.$minApi.updateGoodsPosiChngDetail(this.item.id, this.item).then(res => {
						uni.showToast({
						    title: '修改成功',
						    duration: 2000
						})
						
						setTimeout(() => {
							uni.redirectTo({
								url: `/pages/todo/goodsPosiChngBill/detail?id=${this.goodsPosiChngBillId}`
							})
						}, 2000)
				
					}).catch(err => {
						uni.showToast({
							icon: 'none',
						    title: '修改失败',
						    duration: 2000
						})
					}).finally(() => {
						this.loading = false
					})
				} else {
										
					// this.item.goodsPosiChngDetailID = Number(this.item.goodsPosiChngDetailID)
					_this.$minApi.createGoodsPosiChngDetail(this.item).then(res => {
						uni.showToast({
						    title: '添加成功',
						    duration: 2000
						})
						
						setTimeout(() => {
							uni.redirectTo({
								url: `/pages/todo/goodsPosiChngBill/detail?id=${this.goodsPosiChngBillId}`
							})
						}, 2000)
					}).catch(err => {
						uni.showToast({
							icon: 'none',
						    title: '添加失败',
						    duration: 2000
						})
						console.log(err)
					}).finally(() => {
						this.loading = false
					})
				}
				
			},
			hideModal(e) {
				this.isShowGoodsDialog = false
			},
			selectGoods() {	
				this.isShowGoodsDialog = true
			},
			selectedGoods(val) {
				this.isShowGoodsDialog = false
				if (val) {
					
					if (val) {
						
						this.item.inStockGoodsID = val.goodsID
						this.item.goodsPosi = val.goodsPosi
						this.item.batchNum = val.batchNum						
						this.item.actualQty = 0
						this.item.actualWeight = 0
						this.item.qty = 0
						this.item.weight = 0
						this.item.isInputTaskQuantity='否'
						this.item.isPayGoodsInFee='否'
						this.item.isPayStockFee='否'
						this.item.oldName=''
						
						this.item.inStockGoodsDetail.goodsName = val.goodsName						
						this.item.inStockGoodsDetail.qtyUnit = val.qtyUnit
						this.item.inStockGoodsDetail.weightUnit = val.weightUnit						
						this.item.inStockGoodsDetail.spec = val.spec
						this.item.inStockGoodsDetail.model = val.model
						this.item.inStockGoodsDetail.materialCode = val.materialCode
						this.item.inStockGoodsDetail.maker = val.maker
						this.item.inStockGoodsDetail.frzQty = val.frzQty
						this.item.inStockGoodsDetail.frzWeight = val.frzWeight						
						
					}
				}
				
			},
			backedPage() {
				uni.redirectTo({
					url: `/pages/goodsPosiChngBill/detail`
				})
			}
		}
	}
</script>

<style>

</style>
